package com.netflexitysolutions.amazonws.ec2.internal.operations;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.amazonaws.ec2.doc._2009_04_04.AvailabilityZoneItemType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeAvailabilityZonesResponseType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeAvailabilityZonesSetItemType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeAvailabilityZonesSetType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeAvailabilityZonesType;
import com.netflexitysolutions.amazonws.ec2.AvailabilityZone;
import com.netflexitysolutions.amazonws.ec2.EC2;
import com.netflexitysolutions.amazonws.ec2.internal.ModelUtil;

public class DescribeAvailabilityZonesOperation extends ObjectSetOperationExecutor<DescribeAvailabilityZonesType, List<AvailabilityZone>> {

	public DescribeAvailabilityZonesOperation(EC2 ec2, Set<String> zoneNames) {
		super(ec2, zoneNames);
	}

	@Override
	protected List<AvailabilityZone> call(DescribeAvailabilityZonesType request) {

		request.setAvailabilityZoneSet(createZoneNamesElement());
		
		DescribeAvailabilityZonesResponseType response = getEC2().getService().describeAvailabilityZones(request);
		return ModelUtil.toDomainType(response.getAvailabilityZoneInfo());
	}


	private DescribeAvailabilityZonesSetType createZoneNamesElement() {

		if (objectIds == null) {	
			return null;
		}
		
		DescribeAvailabilityZonesSetType element = new DescribeAvailabilityZonesSetType();
		List<DescribeAvailabilityZonesSetItemType> list = element.getItem();
		for (String zoneName : objectIds) {
			DescribeAvailabilityZonesSetItemType item = new DescribeAvailabilityZonesSetItemType();
			item.setZoneName(zoneName);
			list.add(item);
		}

		return element;
	}

}
